package com.gf.snoopy.service;

import java.util.List;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gf.snoopy.domain.SubProject;
import com.gf.snoopy.dto.SubProjectDto;
import com.gf.snoopy.mapper.SubProjectMapper;
import com.gf.snoopy.platform.exception.PokeErrorCode;
import com.gf.snoopy.platform.exception.PokeException;
import com.gf.snoopy.util.ConvertUtil;
import com.gf.snoopy.util.page.Page;
import com.gf.snoopy.util.page.PagedList;
import com.gf.snoopy.util.page.PaginationUtil;

@Service
public class SubProjectService {
	
	@Resource
	private SubProjectMapper subProjectMapper;

	/**
	 * 分页查询项目下的子项目
	 * @param projectId
	 * @param query
	 * @param page
	 * @return
	 * @throws PokeException
	 */
	public PagedList<SubProjectDto> getProjectPage(String projectId, SubProjectDto query,Page page) throws PokeException{
		if(query==null || page==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"查询参数错误");
		}
		final SubProject dbQuery = ConvertUtil.convert(query,SubProject.class);
		if(StringUtils.isNotBlank(projectId)){
			dbQuery.setProjectId(projectId);
		}		
		PagedList<SubProject> voteInCondition =  PaginationUtil.pagedQuery(page, new Callable<List<SubProject>>() {            
            public List<SubProject> call() throws Exception {
                return subProjectMapper.queryByCondition(dbQuery);
            }
        });
		
		return new PagedList<SubProjectDto>(voteInCondition.getPage(),
				ConvertUtil.convertList(voteInCondition.getList(),
						SubProjectDto.class));
	}
	
	/**
	 * 新增记录
	 * @param data
	 * @throws PokeException
	 */
	public void save(SubProjectDto data) throws PokeException{
		SubProject convert = ConvertUtil.convert(data, SubProject.class);
		if(convert==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"subProject数据出错");
		}
		subProjectMapper.insert(convert);
	}
	
	/**
	 * 更新记录
	 * @param data
	 * @throws PokeException
	 */
	public void update(SubProjectDto data) throws PokeException{
		SubProject convert = ConvertUtil.convert(data, SubProject.class);
		if(convert==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"subProject数据出错");
		}
		subProjectMapper.updateByPrimaryKeySelective(convert);
	}
	
	/**
	 * 查找结果
	 * @param id
	 * @return
	 * @throws PokeException
	 */
	public SubProjectDto findById(Integer id) throws PokeException{
		if(id==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"查询的subProjectId为空");
		}
		SubProject project = subProjectMapper.selectByPrimaryKey(id);
		return ConvertUtil.convert(project, SubProjectDto.class);
	}
	
	/**
	 * 根据Id删除记录
	 * @param id
	 * @throws PokeException
	 */
	public void deleteById(Integer id) throws PokeException{
		if(id==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"删除的subProjectId为空");
		}
		subProjectMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 根据ProjectId删除记录
	 * @param projectId
	 * @throws PokeException
	 */
	public void deleteByPorjectId(Integer projectId) throws PokeException{
		if(projectId==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"projectId为空");
		}
		subProjectMapper.deleteByProjectId(projectId);
	}	
	
	/**
	 * 查询所有子项目
	 * @return
	 * @throws PokeException
	 */
	public List<SubProjectDto> findAllSubProject() throws PokeException{
		List<SubProject> subProjects = subProjectMapper.queryByCondition(new SubProject());
		return ConvertUtil.convertList(subProjects, SubProjectDto.class);
	}
	
	public SubProjectMapper getSubProjectMapper() {
		return subProjectMapper;
	}

	public void setSubProjectMapper(SubProjectMapper subProjectMapper) {
		this.subProjectMapper = subProjectMapper;
	}
	
}
